﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.CodeDom;

namespace ProgNetExtensions.DataProviderTools
{
    [DesignerSerializer(typeof(DataProviderSerializer), typeof(CodeDomSerializer))]
    public class DataProvider : ICloneable
    {
        public string Name
        {
            get;
            set;
        }
        
        [DefaultValue(false)]
        public bool IsStatic
        {
            get;
            set;
        }
        
        [TypeConverter(typeof(ProgNetUtils.TypeConverters.ArrayStringConverter))]
        public string[] ValueMembers
        {
            get;
            set;
        }

        [TypeConverter(typeof(ProgNetUtils.TypeConverters.ArrayStringConverter))]
        public string[] DisplayMembers
        {
            get;
            set;
        }

        /// <summary>
        /// Character used to join columns for ValueMember and DisplayMember
        /// </summary>
        [DefaultValue('|')]
        public char LinkColumnSeparator
        {
            get;
            set;
        }

        [TypeConverter(typeof(ProgNetUtils.TypeConverters.ArrayStringConverter))]
        public string[] Tables
        {
            get;
            set;
        }
        [DefaultValue((string)null)]
        public string Conditions
        {
            get;
            set;
        }

        private DataProviderColumnCollection _Columns;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DataProviderColumnCollection Columns
        {
            get { return _Columns; }
        }

        [TypeConverter(typeof(ProgNetUtils.TypeConverters.ArrayStringConverter))]
        [DefaultValue(null)]
        public string[] OrderColumns
        {
            get;
            set;
        }
        
        public DataProvider()
        {
            _Columns = new DataProviderColumnCollection();
            LinkColumnSeparator = '|';
        }

        #region ICloneable Members

        public object Clone()
        {
            return this.CloneObj();
        }

        #endregion

        public override string ToString()
        {
            return Name;
        }
    }

    public enum DataProviderDisplayType
    {
        LookUp,
        Combo,
        Dialog,
        Auto
    }


    public class DataProviderSerializer : CodeDomSerializer
    {
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            object codeObject = base.Serialize(manager, value);
            try
            {
                string name = (value as DataProvider).Name;
                // Get the container with statements
                CodeStatementCollection statements = (CodeStatementCollection)codeObject;
                int count = statements.Count;

                for (int i = 0; i < count; i++)
                {
                    System.Collections.Specialized.ListDictionary dis = statements[i].UserData as System.Collections.Specialized.ListDictionary;
                    if (statements[i].GetType() == typeof(CodeExpressionStatement))
                    {
                        statements.Insert(i, new CodeCommentStatement(name + " Properties"));
                        break;
                    }
                }

                statements.Insert(0, new CodeCommentStatement(""));
                statements.Insert(0, new CodeCommentStatement("DataProvider - " + name));
                statements.Insert(0, new CodeCommentStatement(""));
            }
            catch { }
            return codeObject;
        }
    }
}
